package com.eastcompeace.controller;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eastcompeace.domain.User;
import com.eastcompeace.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Component
@Path("/users")
@Api(value = "/users") // Enables Swagger Documentation
@Produces(MediaType.APPLICATION_JSON)
public class UserController {

	@Autowired
	private UserService userService;

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public User save(final User user) {
		return userService.save(user).get();
	}

	@GET
	@Path("/{id}")
	@ApiOperation(value = "Finds an exist user with passed ID")
	@ApiImplicitParams({ @ApiImplicitParam(name = "tokens", paramType = "header", dataType = "String") })

	public User findById(@PathParam("id") final long userId) throws Exception {
		throw new Exception();
	}

	@GET
	@ApiOperation(value = "Find all users", response = User.class)
	public List<User> findAll() {
		return userService.findAll().get();
	}

	@PUT
	@Path("/{id}")
	@ApiOperation(value = "Updates an exist user with passed user fields")
	public User update(@PathParam("id") final long id, final User user) {
		return userService.update(id, user).get();
	}

	@DELETE
	@Path("/{id}")
	@ApiOperation(value = "Deletes an exist user with ID")
	public void deleteById(@PathParam("id") final long userId) {
		userService.deleteById(userId);
	}
}